<template>
	<view class="container">
		<view class="logo-box">
			<image src="@/static/logo.png" mode="heightFix" class="logo"></image>
		</view>
		<view class="login-style-box">
			<button class="style-item wechat-auth-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneHandler"
				v-if="agree">手机号码快捷登录</button>
			<button class="style-item wechat-auth-btn" @click="checkAgree" v-else>手机号码快捷登录</button>
			<view class="style-item" v-show="1 == 2">使用其他手机号登录</view>
		</view>
		<view class="bottom">
			<view class="agree">
				<checkbox :checked="agree" color="#07C160" style="transform:scale(0.7)" @click="agreeClick" />
				<text>同意</text>
				<text class="href" @click="goParse('用户服务协议','service')">《用户服务协议》</text>
				和
				<text class="href" @click="goParse('隐私政策','secret')">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		wxAppId,
		wxAppSecrect
	} from '@/utils/config.js'
	import WXBizDataCrypt from '@/utils/WXBizDataCrypt.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		getWeixinAuth
	} from '@/api/public.js'
	export default {
		data() {
			return {
				redirect: undefined,
				userservices: 'https://api.yongtenghr.cn/userservices.pdf',
				secret: 'https://api.yongtenghr.cn/secret.pdf',
				agree: false,

				inviteCode: '',
			}
		},
		onLoad() {
			this.redirect = this.$Route.query.redirect
			this.inviteCode = uni.getStorageSync('invite_code')
		},
		methods: {
			agreeClick() {
				this.agree = !this.agree
			},
			checkAgree() {
				uni.showToast({
					icon: 'none',
					title: '请同意用户服务协议和隐私政策'
				})
			},
			getPhoneHandler(e) {
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					this.wxxcx_code2 = e.detail.code
					this.wxxcxLogin()
				}
			},
			wxxcxLogin() {
				var that = this

				uni.showLoading({
					title: `登录中`
				})

				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						that.wxxcx_code = loginRes.code

						uni.getUserInfo({
							success: function(res2) {
								that.$store
									.dispatch('login', {
										code: that.wxxcx_code,
										codeMobile: that.wxxcx_code2,
										encryptedData: res2.encryptedData,
										iv: res2.iv,
										inviteCode: that.inviteCode
									})
									.then(res => {
										uni.hideLoading()
										if (that.redirect) {
											that.$Router.replace({
												path: that.redirect
											})
										} else {
											that.$Router.pushTab({
												name: 'home'
											})
										}
									})
									.catch(err => {
										uni.hideLoading()
										that.$tools.msg(`登录失败`)
									})
							}
						})
					}
				})
			},


			showPdf(title, url) {
				const reg = /(\w*)《(.*)》/g;
				let str = url.split('/')[url.split('/').length - 1].split('.')[0];
				const titleClone = title ? title.replace(reg, '$1 $2 ') : str;

				uni.getSystemInfo({
					success(sysRes) {
						if (sysRes.platform == 'ios') {
							uni.navigateTo({
								url: `./webview?title=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`
							})
						} else {
							console.log(url, title)
							uni.showLoading({
								title: '加载中...',
								mask: true,
							})
							uni.downloadFile({
								url: url,
								success(res) {
									const filePath = res.tempFilePath
									let arr = filePath.split('.')
									const mime = arr[arr.length - 1]

									const fs = uni.getFileSystemManager()
									// 修改文件名字，仅安卓可以，ios无权限
									// 小米k40s，依然提示权限不足，所以暂时去除修改文件名称功能
									console.log(filePath, `${wx.env.USER_DATA_PATH}/${titleClone}`)
									uni.openDocument({
										filePath: filePath,
										fileType: mime,
										success: function(res) {
											console.log('打开文档成功')
										},
										fail() {
											uni.showToast({
												title: '打开文件失败',
												icon: 'none',
												duration: 2000
											})
										}
									})
									// fs.rename({
									// 	oldPath: filePath,
									// 	newPath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
									// 	success() {
									// 		uni.openDocument({
									// 			filePath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
									// 			fileType: mime,
									// 			success: function(res) {
									// 				console.log('打开文档成功')
									// 			},
									// 			fail: function() {
									// 				uni.showToast({
									// 					title: '打开文件失败',
									// 					icon: 'none',
									// 					duration: 2000
									// 				})
									// 			}
									// 		})
									// 	},
									// 	fail: function() {
									// 		uni.showToast({ title: '保存文件失败', icon: 'none', duration: 2000 })
									// 	}
									// })
								},
								fail(fileErr) {
									uni.showToast({
										title: '下载文件失败',
										icon: 'none',
										duration: 2000
									})
								},
								complete() {
									uni.hideLoading()
								}
							})
						}
					},
					fail(sysErr) {
						uni.showToast({
							title: '获取系统信息失败',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			goParse(title, type) {
				this.$Router.push({
					path: '/pages/common/parse',
					query: {
						title,
						type
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		height: 100%;
		flex-direction: column;

		.logo-box {
			text-align: center;
			margin-top: 100rpx;
			min-height: 100rpx;

			.logo {
				width: auto;
				margin: 0 auto;
				height: 100rpx;
			}
		}

		.login-style-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.style-item {
				width: 90%;
				border-radius: 10rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				cursor: pointer;
			}

			.wechat-auth-btn {
				background-color: #07C160;
				color: #fff;
				font-size: 24rpx;
			}
		}

		.bottom {
			margin-bottom: 160rpx;

			.agree {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 80%;
				margin: 0 auto;
				text-align: center;

				.href {
					color: #07C160;

					&:active {
						background-color: #ccc;
					}
				}
			}
		}
	}
</style>